<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script type="text/javascript">
	$(function(){
		$("#search").autocomplete({
			source:function(request, response){
				$.ajax({
					type:"post",
					url:"major.do",
					data:"command=autoComplete&&search="+request.term,
					
					success:function(data){
						response(data.sList);
					}
				});
			}
		});
		
		$("#searchBtn").click(function(){
			var insert=$("#search").val().replace(/\s/g, "");
			if(insert==""){
				alert("검색할 학과를 입력해주세요.");
				return;
			}
			getList("");
		});
	});
	
	function getList(page){
		$.ajax({
			type:"post",
			url:"major.do",
			data:"command=getPopupSearchedUnit&&search="+$("#search").val()+"&&page="+page,
			dataType:"json",
			
			success:function(data){
				var mList=data.sList.majorunitList;
				var listHtml="";
				
				for(var i=0; i<mList.length;i++){
					listHtml+="<a href=\"javascript:returnBefore('"+mList[i].majorunit_no+"', '"+mList[i].majorunit_name+"')\">"+mList[i].majorunit_name+"</a><p>";
				}
				
				$('#resultView').html(listHtml);
				
				paging(data);
			}
		});
	}
	
	function returnBefore(mNo, mName){
		opener.document.getElementById("majorunit_no").value=mNo;
		opener.document.getElementById("majorunit_name").value=mName;
		
		self.close();
	}
	
	function paging(data){
		var page=data.sList.pageManager;
		var pageHtml="";
		
		if(page.currGroup==1){ //현재 그룹이 1이라면
			pageHtml+="<img src='../img/button/page_begin.gif'>&nbsp"; //<a> 없음
		}else{
			pageHtml+="<a href='javascript:getList("+1+")'>&nbsp<img src='../img/button/page_begin.gif'>&nbsp</a>";
		}

		if(page.existOfPreviousGroup){ // 앞 그룹이 있다면
			pageHtml+="<a href='javascript:getList("+(page.firstPageInCurrGroup-page.pagePerGroup)+")'>&nbsp<img src='../img/button/page_prev.gif'>&nbsp</a>";
		}else{
			pageHtml+="&nbsp<img src='../img/button/page_prev.gif'>&nbsp"; //<a> 없음
		}
		
		// 현재 그룹의 첫페이지부터 마지막페이지까지
		for(var pageIndex=page.firstPageInCurrGroup;pageIndex<=page.lastPageInCurrGroup;pageIndex++){
			if(pageIndex==page.currPage){ // 출력하는 페이지가 현재 페이지라면
				pageHtml+= "<span class='pageCurrText'>&nbsp;"+pageIndex+"&nbsp;</span>"; //<a> 없음
			}else{
				pageHtml+= "<span class='pageCurrText'>&nbsp;</span><a href='javascript:getList("+pageIndex+")' class='pageTotalText'>"+pageIndex+"</a><span>&nbsp;</span>";
			}
		}

		// 뒷 그룹이 있다면
		if(page.existOfNextGroup){
			pageHtml+="<a href='javascript:getList("+(page.lastPageInCurrGroup+1)+")' class='pageCurrText'>&nbsp<img src='../img/button/page_next.gif'>&nbsp</a>";
		}else{
			pageHtml+="&nbsp<img src='../img/button/page_next.gif'>&nbsp"; //<a> 없음
		}

		// 마지막 그룹이라면
		if(page.currGroup==page.totalGroup){
			pageHtml+="&nbsp<img src='../img/button/page_end.gif'>&nbsp"; //<a> 없음
		}else{
			pageHtml+="<a href='javascript:getList("+page.totalPage+")' class='pageText'>&nbsp<img src='../img/button/page_end.gif'>&nbsp</a>";
		}
		//pagingDiv에 표시
		$('#pagingDiv').html(pageHtml);
	}
</script>
</head>
<body>
<!-- 학과 선택 팝업 창 -->
학과 입력
<input type="text" name="search" id="search">
<a href="#" id="searchBtn">검색</a>
	<div id="resultView"></div>
	<div id="pagingDiv"></div>
</body>
</html>